﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hjp.FrameWork.Common;
using Wjtour.Domain;
using Wjtour.BLL;

namespace Wjtour.Web.Admin.sysadmin
{
    public partial class addedi : BasePage
    {
        protected TextBox adminname;
        protected TextBox adminpass;
        protected DropDownList role;
        protected Button submit;
        protected int id = 0;
        Wjtour.Domain.sysadmin entity = new Domain.sysadmin();
        sysadmin_bll _bll = new sysadmin_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            submit.Click += new EventHandler(submit_Click);
            if (!IsPostBack)
            {
                BindDropdown();
                if (id > 0)
                {
                    entity = _bll.Load(id);
                    if (entity != null)
                    {
                        adminname.Text = entity.adminname;
                        role.SelectedValue = entity.role.ToString();
                    }
                }
            }
        }

        void BindDropdown()
        {
            webrole_bll rolebll = new webrole_bll();
            List<webrole> roles = rolebll.GetAll<webrole>("select * from webrole").ToList();
            foreach (var item in roles)
            {
                role.Items.Add(new ListItem(item.title, item.id.ToString()));
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            entity.id = id;
            entity.adminname = adminname.Text;
            entity.adminpass = Utils.MD5(adminpass.Text);
            entity.role = Utils.StrToInt(role.SelectedValue, 0);
            if (entity.id == 0)
                entity.addtime = DateTime.Now;

            if (string.IsNullOrWhiteSpace(entity.adminpass))
            {
                Utils.Alert(this.Page, "密码不能为空！");
                return;
            }

            bool b = _bll.Add(entity);
            if (b)
            {
                SaveLog(id == 0 ? "Add" : "edit");
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "list.aspx", false);
            }
        }
    }
}